package com.kaizeli.website.util;

import java.util.*;

/**
 * 字段映射工具类
 * 用于将Excel中文表头映射到数据库字段名
 */
public class FieldMappingUtil {
    
    /**
     * 员工表字段映射（Excel表头 -> 数据库字段）
     */
    public static final Map<String, String> EMPLOYEE_FIELD_MAPPING = new HashMap<String, String>() {{
        // 基本信息
        put("档案号", "archive_no");
        put("项目编号", "project_code");
        put("项目编码", "project_code");
        put("客户单位", "client_unit");
        put("所在部门", "department");
        put("部门", "department");
        put("所在团队", "team");
        put("团队", "team");
        put("岗位名称", "position");
        put("岗位", "position");
        put("员工类型", "employee_type");
        put("人员类型", "employee_type");
        
        // 个人信息
        put("姓名", "name");
        put("性别", "gender");
        put("婚否", "marital_status");
        put("婚姻状况", "marital_status");
        put("籍贯", "native_place");
        put("民族", "ethnicity");
        put("政治面貌", "political_status");
        put("身份证号", "id_card");
        put("身份证", "id_card");
        put("生日", "birthday");
        put("出生日期", "birthday");
        
        // 地址信息
        put("户籍地址", "registered_address");
        put("户籍", "registered_address");
        put("现居住地址", "current_address");
        put("现住址", "current_address");
        put("联系电话", "phone");
        put("电话", "phone");
        put("手机号", "phone");
        put("手机", "phone");
        put("微信号", "wechat");
        put("微信", "wechat");
        put("邮箱", "email");
        put("电子邮箱", "email");
        
        // 工作信息
        put("入职时间", "hire_date");
        put("入职日期", "hire_date");
        put("转正时间", "confirmation_date");
        put("转正日期", "confirmation_date");
        put("试用状态", "probation");
        put("试用期", "probation");
        
        // 学历信息
        put("学历/学位", "education");
        put("学历", "education");
        put("学位", "education");
        put("所学专业", "major");
        put("专业", "major");
        put("毕业院校", "university");
        put("院校", "university");
        put("学校", "university");
        put("毕业时间", "graduation_date");
        put("毕业日期", "graduation_date");
        put("技能证书", "skill_certificates");
        put("证书", "skill_certificates");
        
        // 薪酬信息
        put("基本工资", "base_salary");
        put("工资", "base_salary");
        put("小时工资率", "hourly_rate");
        put("时薪", "hourly_rate");
        
        // 紧急联系人
        put("紧急联系人", "emergency_contact");
        put("紧急联系电话", "emergency_phone");
        
        // 银行信息
        put("银行卡号", "bank_account");
        put("银行卡", "bank_account");
        put("账号", "bank_account");
        put("开户行", "bank_name");
        put("银行", "bank_name");
        
        // 合同信息
        put("是否续签合同", "contract_renewed");
        put("续签合同次数", "renewal_count");
        put("劳动合同开始时间", "latest_contract_start");
        put("合同开始", "latest_contract_start");
        put("劳动合同结束时间", "latest_contract_end");
        put("合同结束", "latest_contract_end");
        put("签合同公司", "contract_company");
        put("签约公司", "contract_company");
        
        // 备注
        put("备注", "remarks");
        put("说明", "remarks");
    }};
    
    /**
     * 项目表字段映射
     */
    public static final Map<String, String> PROJECT_FIELD_MAPPING = new HashMap<String, String>() {{
        put("项目编码", "project_code");
        put("项目编号", "project_code");
        put("项目名称", "project_name");
        put("子项目名称", "project_subname");
        put("子项目", "project_subname");
        put("项目描述", "description");
        put("描述", "description");
        put("开始时间", "start_date");
        put("开始日期", "start_date");
        put("结束时间", "end_date");
        put("结束日期", "end_date");
        put("项目预算", "project_budget");
        put("预算", "project_budget");
        put("预计成本", "estimated_cost");
        put("成本", "estimated_cost");
        put("项目类型", "project_type");
        put("类型", "project_type");
        put("状态", "status");
        put("项目状态", "status");
    }};
    
    /**
     * 合同人员表字段映射
     */
    public static final Map<String, String> CONTRACT_PERSONNEL_FIELD_MAPPING = new HashMap<String, String>() {{
        put("项目编号", "project_code");
        put("项目编码", "project_code");
        put("合同号", "contract_no");
        put("合同编号", "contract_no");
        put("人员姓名", "personnel_name");
        put("姓名", "personnel_name");
        put("人员类型", "personnel_type");
        put("类型", "personnel_type");
        put("岗位", "position");
        put("岗位名称", "position");
        put("部门", "department");
        put("科室", "team");
        put("开始时间", "start_date");
        put("开始日期", "start_date");
        put("结束时间", "end_date");
        put("结束日期", "end_date");
        put("单价", "unit_price");
        put("价格", "unit_price");
        put("单价类型", "price_type");
        put("价格类型", "price_type");
        put("状态", "status");
        put("备注", "remark");
    }};
    
    /**
     * 获取字段映射
     * @param tableType 表类型（employee/project/contract_personnel）
     * @return 字段映射Map
     */
    public static Map<String, String> getFieldMapping(String tableType) {
        switch (tableType.toLowerCase()) {
            case "employee":
                return EMPLOYEE_FIELD_MAPPING;
            case "project":
                return PROJECT_FIELD_MAPPING;
            case "contract_personnel":
                return CONTRACT_PERSONNEL_FIELD_MAPPING;
            default:
                throw new IllegalArgumentException("不支持的表类型: " + tableType);
        }
    }
    
    /**
     * 映射中文表头到数据库字段
     * @param tableType 表类型
     * @param excelHeader Excel表头（中文）
     * @return 数据库字段名，如果找不到返回null
     */
    public static String mapToDatabaseField(String tableType, String excelHeader) {
        Map<String, String> mapping = getFieldMapping(tableType);
        // 去除空格后匹配
        String trimmedHeader = excelHeader != null ? excelHeader.trim() : "";
        return mapping.get(trimmedHeader);
    }
    
    /**
     * 批量映射表头
     * @param tableType 表类型
     * @param excelHeaders Excel表头列表
     * @return 字段名 -> 列索引的映射
     */
    public static Map<String, Integer> mapHeaders(String tableType, List<String> excelHeaders) {
        Map<String, Integer> result = new HashMap<>();
        for (int i = 0; i < excelHeaders.size(); i++) {
            String dbField = mapToDatabaseField(tableType, excelHeaders.get(i));
            if (dbField != null) {
                result.put(dbField, i);
            }
        }
        return result;
    }
    
    /**
     * 获取所有可用字段的说明（用于生成模板）
     * @param tableType 表类型
     * @return 字段说明映射
     */
    public static Map<String, String> getFieldDescriptions(String tableType) {
        Map<String, String> descriptions = new HashMap<>();
        
        if ("employee".equalsIgnoreCase(tableType)) {
            descriptions.put("姓名", "必填");
            descriptions.put("身份证号", "必填，18位");
            descriptions.put("性别", "男/女");
            descriptions.put("部门", "所在部门");
            descriptions.put("岗位", "岗位名称");
            descriptions.put("电话", "手机号");
            descriptions.put("入职时间", "YYYY-MM-DD");
            // 更多字段说明...
        } else if ("project".equalsIgnoreCase(tableType)) {
            descriptions.put("项目编码", "必填，唯一");
            descriptions.put("项目名称", "必填");
            descriptions.put("项目类型", "1-人力项目, 2-整包项目, 3-分包项目");
            descriptions.put("开始日期", "YYYY-MM-DD");
            descriptions.put("结束日期", "YYYY-MM-DD");
            descriptions.put("项目预算", "数字");
            // 更多字段说明...
        } else if ("contract_personnel".equalsIgnoreCase(tableType)) {
            descriptions.put("项目编号", "必填");
            descriptions.put("人员姓名", "必填");
            descriptions.put("人员类型", "正式员工/外协人员/临时工");
            descriptions.put("开始日期", "YYYY-MM-DD");
            descriptions.put("结束日期", "YYYY-MM-DD");
            descriptions.put("单价", "数字");
            // 更多字段说明...
        }
        
        return descriptions;
    }
}

